<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/21
 * Time: 13:55
 */

namespace Admin\Controller;


use Think\Controller;

class SdfpController extends AdminController
{
    //对父类构造函数的补充
    public function _myinitialize()
    {
        $this->db = M('sdfp');
    }

    public function lst()
    {
        $session_where=I('session_where');//不为空，则表明是从add/edit/del中跳转过来的，刚查询条件从session中取。
        if(empty($session_where)){
            //查询条件
            $where['status'] = array('gt', 0);

            //所有出票方列表
            $cpf_arr = $this->db->where($where)->getField('cpf', true);
            $cpf_arr = array_values(array_unique($cpf_arr));
            $this->assign('cpf_arr', $cpf_arr);

            //所有供应商列表
            $gys_arr = $this->db->where($where)->getField('gys', true);
            $gys_arr = array_values(array_unique($gys_arr));
            $this->assign('gys_arr', $gys_arr);

            //所有收票方列表
            $spf_arr = $this->db->where($where)->getField('spf', true);
            $spf_arr = array_values(array_unique($spf_arr));
            $this->assign('spf_arr', $spf_arr);

            $month = I('month');
            $year = I('year');
            if (!empty($month) && !empty($year)) { //所属月份
                $ssyf = $year . $month;
                $where['yf'] = $ssyf;
            }

            $cpf=I('cpf');
            $gys=I('gys');
            $spf=I('spf');
            if(!empty($cpf)) $where['cpf']=$cpf;
            if(!empty($gys)) $where['gys']=$gys;
            if(!empty($spf)) $where['spf']=$spf;

            if (count($where) === 1) { //此时查询条件仅为status=1，表示没有别的查询条件，则默认查询条件为：只显示当月的数据
                $current_m = date("Ym", time());//默认情况下显示当月的数据
                $where['yf'] = $current_m;
            }
            //将查询条件存入session中，方便更新时跳转
            session(CONTROLLER_NAME."where",$where);
        }else{
            $where=session(CONTROLLER_NAME."where");
        }
        //列表数据
        $lsts = $this->db->where($where)->order("updatetime desc")->select();
        $this->assign('lsts', $lsts);//列表数据

        $this->display();
    }

    //新增 www.glxt.com/admin/khzy/add
    public function add()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['updatetime']=time();
                $data['status'] = 1;
                $sl=str_replace('%','',$data['sl']);//去掉%
                $data['se']=$data['je']*$sl/100;//税额=不含税金额*税率;
                $data['jshj']=$data['se']+$data['je'];//价税合计=不含税金额+税额
                $data['yf']=date('Ym',strtotime($data['sssj']));
            }
            $rid = $this->db->data($data)->add();

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }

    //修改
    public function edit()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['updatetime']=time();
                $data['status'] = 1;
                $sl=str_replace('%','',$data['sl']);//去掉%
                $data['se']=$data['je']*$sl/100;//税额=不含税金额*税率;
                $data['jshj']=$data['se']+$data['je'];//价税合计=不含税金额+税额
                $data['yf']=date('Ym',strtotime($data['sssj']));
            }
            $rid = $this->db->data($data)->save();

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        } else { //回显
            $id = I('id');
            $data = $this->db->where(array('id' => $id))->find();
            $this->ajaxReturn($data);
        }
    }

    //删除不是物理删除，只是状态的改变
    public function del()
    {
        $id = I('id');
        if (!empty($id)) {
            $data['status'] = 0;
            $data['updatetime'] = time();
            $this->db->where(array('id' => $id))->setField($data);
            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }
}